<template>
  <div>
    <el-dialog :visible.sync='isShow' title='交易分配' width="800px" @open="openDialogInit" @close="resetForm" :close-on-click-modal=false>
      <div v-loading="loading">
        <el-card>
          <el-form :model="formInline" ref="formInline" size="mini" :rules="rules" label-position="left" label-width="125px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label='应用编号'>
                  <el-select v-model="formInline.appnum" disabled>
                     <el-option v-for="item in appQueryList" :key="item.AppNum" :label="item.AppNum+'-'+item.AppName" :value="item.AppNum">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label='菜单试图编号'>
                  <el-select v-model="formInline.MenuViewNum" @change="menuViewChange" placeholder="请选择菜单试图编号">
                    <el-option v-for="item in MenuViewList" :key="item.MenuViewNum" :label="item.MenuViewNum+'-'+item.MenuViewName" :value="item.MenuViewNum">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="角色编号:" prop="TranGroupNum">
                  <el-input v-model="formInline.trangroupnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="角色名称:" prop="trangroupname">
                  <el-input v-model="formInline.trangroupname" disabled></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="法人号:" prop="LegalPersonNum">
                  <el-input v-model="formInline.legalpersonnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人名称:" prop="LegalPersonName">
                  <el-input v-model="formInline.LegalPersonName" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card style="margin-top:10px;">
          <el-tree :data="pageList" :props="props" show-checkbox :default-checked-keys="checkPageList" node-key="menunum" @check-change="handleCheckChange" ref="tree">
          </el-tree>
        </el-card>
        <el-row style="margin:20px 0;text-align:right">
          <el-button size="mini" @click="isShow=false">取消</el-button>
          <el-button type="primary" size="mini" @click="TranGroupToTran">确认</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/common/utils/request";
import tranGroup from "@/message/userManage/role/tranGroup";
import tranGroupNum from "@/message/branchManage/tranGroupManage/tranGroupNum";
import menuView from "@/message/branchManage/tranGroupManage/menuView";
import tranGroupQuery from "@/message/userManage/role/tranGroupQuery";
import toolBox from "@/common/utils/toolBox";

export default {
  props: { option: Object },
  data() {
    return {
      loading: false,
      appQueryList: [],
      formInline: {},
      GroupTranList: [],
      AppNum: "",
      MenuViewList: [],
      props: {
        label: "menuname",
        children: "children"
      },
      pageList: [], // 交易所有数据
      checkPageList: [], // 选中的交易
      rules: {
        trangroupname: [
          { required: true, message: "请输入交易组名称", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    isShow: {
      get: function() {
        return this.option.isShow;
      },
      set: function(newValue) {
        this.option.isShow = newValue;
      }
    }
  },
  methods: {
    openDialogInit() {
      this.appQueryList = this.option.appList;
      this.formInline.appnum = this.option.currentRow[0].appnum;
      this.formInline.trangroupnum = this.option.currentRow[0].rolenum;
      this.formInline.trangroupname = this.option.currentRow[0].rolename;
      this.formInline.legalpersonnum = this.$store.getters.LegalPersonNum;
      this.formInline.LegalPersonName = this.$store.getters.LegalPersonName;
      this.menuViewInit();
    },

    // 菜单视图加载
    menuViewInit() {
      let resBody = new menuView();
      resBody.data.AppNum = this.option.currentRow[0].appnum;
      request(resBody)
        .then(response => {
          if (response.APP_HEAD.ReturnCode === "000000") {
            this.MenuViewList = response.RspInfo.Result || [];
            this.formInline.MenuViewNum = this.MenuViewList[0].MenuViewNum;
            this.menuViewChange(this.formInline.MenuViewNum);
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
            this.loading = false;
          }
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },
    // 树形下拉框初始化查询
    tranGroupQuery(val) {
      let resBody = new tranGroupQuery();
      resBody.data.AppNum = this.option.currentRow[0].appnum;
      resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
      resBody.data.RoleNum = this.formInline.trangroupnum;
      resBody.data.MainBranch = this.$store.getters.BranchNum;
      resBody.data.MainUser = this.$store.getters.UserNum;
      resBody.data.MenuViewNum = val;
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            let pageList1 = response.RspInfo.pageList || [];
            this.pageList = toolBox.formatTreeData(
              pageList1,
              "menunum",
              "parentmenunum"
            );
            console.log(this.pageList);
            let checkPageList1 = response.RspInfo.pageList1 || [];
            let temp = [];
            checkPageList1.forEach(element => {
              temp.push(element.privlegeid);
            });
            this.checkPageList = temp;
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
          this.loading = false;
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 菜单试图值改变事件
    menuViewChange(val) {
      this.tranGroupQuery(val);
    },

    // 树值改变事件
    handleCheckChange(data, checked, indeterminate) {},

    resetForm() {
      this.$refs["formInline"].resetFields();
    },
    TranGroupToTran() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          let keyList = this.$refs.tree.getCheckedNodes();
          this.GroupTranList = [];
          for (let i = 0; i < keyList.length; i++) {
            const element = keyList[i];
            if (element.menutype == '02') {
              let PostList = {
                PrivLegeId: element.menunum
              };
              this.GroupTranList.push(PostList);
            }
          }
          let resBody = new tranGroup();
          resBody.data.AppNum = this.formInline.appnum;
          resBody.data.RoleNum = this.formInline.trangroupnum;
          resBody.data.MenuViewNum = this.formInline.MenuViewNum;
          resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
          resBody.data.MainBranch = this.$store.getters.BranchNum;
          resBody.data.MainUser = this.$store.getters.UserNum;
          resBody.data.GroupTranList = this.GroupTranList;
          request(resBody)
            .then(response => {
              if (response.SYS_HEAD.ReturnCode === "000000") {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: "交易组修改成功!"
                });
                this.isShow = false;
                this.$emit("refreshBrand");
              } else {
                this.loading = false;
                this.$message({
                  type: "error",
                  message: response.SYS_HEAD.ReturnMessage
                });
              }
            })
            .catch(error => {
              this.loading = false;
              console.log(error);
            });
        } else {
          this.$message({
            type: "warning",
            message: "必输项不能为空"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style lang="sass" scoped>

</style>
